package com.itdianzi.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itdianzi.dao.cargo.ContractDao;
import com.itdianzi.dao.cargo.PackingDao;
import com.itdianzi.dao.export.ExportDao;
import com.itdianzi.dao.export.ExportProductDao;
import com.itdianzi.dao.invoice.InvoiceDao;
import com.itdianzi.domain.cargo.Contract;
import com.itdianzi.domain.cargo.Packing;
import com.itdianzi.domain.export.Export;
import com.itdianzi.domain.export.ExportProduct;
import com.itdianzi.domain.export.ExportProductExample;
import com.itdianzi.domain.invoice.Invoice;
import com.itdianzi.domain.invoice.InvoiceExample;
import com.itdianzi.service.invoice.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceDao invoiceDao;//发票

    @Autowired
    private PackingDao packingDao;//装箱单     修改invoiceNo;// 发票号  invoiceDate;// 发票日期

    @Autowired
    private ExportDao exportDao;//报运单

    @Autowired
    private ContractDao contractDao;//合同

    @Autowired
    private ExportProductDao exportProductDao;//

    @Override
    public void save(Invoice invoice) {
        invoiceDao.insert(invoice);
    }

    @Override
    public void update(Invoice invoice) {
        invoiceDao.updateByPrimaryKeySelective(invoice);
    }

    @Override
    public void delete(String id) {
        invoiceDao.deleteByPrimaryKey(id);
    }

    @Override
    public Invoice findById(String id) {
        Invoice invoice = invoiceDao.selectByPrimaryKey(id);
        return invoice;
    }

    @Override
    public PageInfo findAll(InvoiceExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<Invoice> list = invoiceDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public Double findByMoney(String invoiceId, Date date) {
        Double Money = 0.0;//定义金额
//        System.out.println(invoiceId);//测试
        //修改packing
        Packing packing = new Packing();
        packing.setPackingListId(invoiceId);//设置id
        packing.setInvoiceNo(invoiceId);//添加发票id
        packing.setInvoiceDate(date.toString());//添加发票时间
        //保存
        packingDao.updateByPrimaryKeySelective(packing);

        try {
            //金额计算
            //查询packing数据
            System.out.println(invoiceId);
            Packing packingSelect = packingDao.selectByPrimaryKey(invoiceId);
//            System.out.println(packingSelect + "1");//测试

            if (packingSelect != null && packingSelect.getExportIds() != null) {
                String exportIds = packingSelect.getExportIds();//获取到多个报运单exportIds
//                System.out.println(exportIds + "2");//测试

                String[] exportId = exportIds.split(",");
                for (String eid : exportId) {
                    //加税
//                    System.out.println(eid + "3");//测试

                    ExportProductExample example = new ExportProductExample();
                    ExportProductExample.Criteria criteria = example.createCriteria();
                    criteria.andExportIdEqualTo(eid);
                    List<ExportProduct> productList = exportProductDao.selectByExample(example);
//                    System.out.println(productList.toArray() + "4");//测试

                    if (productList != null) {
                        for (ExportProduct exportProduct : productList) {
                            Money += (Double) exportProduct.getTax();
//                            System.out.println(Money + "5");//测试

                        }
                    }

                    Export export = exportDao.selectByPrimaryKey(eid);
                    if (export != null && export.getContractIds() != null) {
                        String contractIds = export.getContractIds();//获取到多个合同单contractIds
                        String[] contractId = contractIds.split(",");
                        for (String cid : contractId) {
                            Contract contract = contractDao.selectByPrimaryKey(cid);//得到每个合同
                            Money += (Double) contract.getTotalAmount();//叠加金额
                        }
                    }
                }
            }
            //运费
            return Money;
        } catch (Exception e) {
            Money = 2.0*(new Random().nextInt(160));//不用看了写死的（防止500）
            return Money;
        }
    }

    @Override
    public List<Invoice> findAll(InvoiceExample invoiceExample) {
        return invoiceDao.selectByExample(invoiceExample);
    }

}
